#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <errno.h>
#include <termios.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>

#define STORAGE_NUM 		5
#define GOODS_NUM			10
#define N 32
#define STORAGE_NO	'1'

#define LED2_ON    0X10
#define LED2_OFF   0X20
#define LED3_ON    0X30
#define LED3_OFF   0X40

#define BUZZER_ON  0x50  
#define BUZZER_OFF 0x60

#define GPX2_CON 0x11000c40
#define GPX2_DAT 0x11000c44
#define LED3CON0 0x11000c20
#define	LED3DAT0 0x11000c24
#define	LED4CON4 0x114001e0
#define	LED4DAT4 0x114001e4
#define	LED5CON5 0x114001e0
#define	LED5DAT5 0x114001e4


struct storage_goods_info
{
	unsigned char goods_type;
	unsigned int goods_count;
};


struct sendEnv
{
	unsigned char head[3];	 //标识位st:
	unsigned char type;		 //数据类型
	unsigned char snum;		 //仓库编号
	unsigned char temp[2];	 //温度	
	unsigned char hum[2];		 //湿度
	unsigned char x;			 //三轴信息
	unsigned char y;			 
	unsigned char z;			 
	unsigned int ill;		 //光照
	unsigned int bet;		 //电池电量
	unsigned int adc; 		 //电位器信息
};	

struct storage_info
{
	unsigned char storage_status;				// 0:open 1:close
	unsigned char led_status;
	unsigned char buzzer_status;
	unsigned char fan_status;
	unsigned char seg_status;
	signed char x;
	signed char y;
	signed char z;
	char samplingTime[20];
	float temperature;
	float temperatureMIN;
	float temperatureMAX;
	float humidity;
	float humidityMIN;
	float humidityMAX;
	float illumination;
	float illuminationMIN;
	float illuminationMAX;
	float battery;
	float adc;
	float adcMIN;
	struct storage_goods_info goods_info[GOODS_NUM];
};

struct msg
{
	long type;
	long msgtype;
	unsigned char text[N];
};
struct env_info_clien_addr
{
	struct storage_info storage_no[STORAGE_NUM];	
};

struct shm_addr
{
	char cgi_status;
	char qt_status;
	struct env_info_clien_addr rt_status;
};


typedef struct linklist
{
	struct sendEnv data;
	struct linklist *next;
}linklist_t;

void sem_p(int semsid,int number)
{
	struct sembuf buf;
	buf.sem_num = number;
	buf.sem_op = -1;
	buf.sem_flg = 0;
	
	semop(semsid,&buf,1);
}

void sem_v(int semsid,int number)
{
	struct sembuf buf;
	buf.sem_num = number;
	buf.sem_op = 1;
	buf.sem_flg = 0;
	
	semop(semsid,&buf,1);
}

void linklist_free(linklist_t **list)
{
	free((*list)->next);
	free(*list);
}
linklist_t *create_linklist(void)
{
	linklist_t *head = (linklist_t *)malloc(sizeof(linklist_t));
	head->next = NULL;
	return head;
}

void add_linklist(linklist_t *list,struct sendEnv newdata)
{
	linklist_t *newl = (linklist_t *)malloc(sizeof(linklist_t));
	newl->next = NULL;
	list->next = newl;
	list->data = newdata;
}

struct sendEnv del_linklist(linklist_t **list)
{
	struct sendEnv old = (*list)->data;
	linklist_t *oldl = *list;
	*list = (*list)->next;
	linklist_free(&oldl);
	return old;
}
